package com.nsn.das.midas.report;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import com.nsn.das.midas.model.mml.data.MMLClassData;
import com.nsn.das.midas.model.mml.data.MMLGroupData;
import com.nsn.das.midas.model.mml.data.MasterListData;
import com.nsn.das.midas.service.MasterlistReport;

public class MlistDotTxt implements MasterlistReport{
	private File file;
	private FileOutputStream fos;
	private List<String> listHead=new ArrayList<String>();
	private List<String> listBody=new ArrayList<String>();	
	@Override
	public void generateFile(MasterListData masterListData) {
		
		// TODO Auto-generated method stub
		createFile();
		insertHeader(masterListData);
		insertBody(masterListData);
		inputToFile();
	}
	private void createFile(){		
		file=new File("c:\\tmp\\mlist.txt");
		try {
			file.createNewFile();
			fos=new FileOutputStream("c:\\tmp\\mlist.txt");			
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}			
	}
	private void insertHeader(MasterListData masterListData){
		listHead.add("MLIST.TXT");
		listHead.add("MASTERLIST: " + masterListData.getName()+ " " + masterListData.getVersion().getVersion());  
		listHead.add("------------------------------------------------------------------------------------------------");      
	}
	private void insertBody(MasterListData masterListData){
		for(MMLClassData cl:masterListData.getClassData()){
			listBody.add(cl.getClassLetter().getLetter()+ " " + cl.getVersion().getVersion());
		}
		for(MMLGroupData gr:masterListData.getGroupes()){			
			listBody.add(gr.getGroupLetter().getClassLetter().getLetter()+gr.getGroupLetter().getLetter()
					+" "+gr.getMMLName()+" "+gr.getVersion().getVersion());
		}
		Collections.sort(listBody);
	}
	private void inputToFile(){
		for(String head:listHead){
			try {
				fos.write((head+"\r\n").getBytes());
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		for(String body:listBody){
			try {
				fos.write((body+"\r\n").getBytes());
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
}